package com.beijiteshop.shop.ui.shopcar.vm

import androidx.databinding.ObservableInt
import androidx.lifecycle.MutableLiveData
import com.beijiteshop.libcommon.base.viewmodel.BaseViewModel
import com.beijiteshop.libcommon.network.ext.*
import com.beijiteshop.libcommon.network.model.onFailure
import com.beijiteshop.libcommon.network.model.onSuccess
import com.beijiteshop.shop.model.entity.ShopCarProductEntity
import com.beijiteshop.shop.model.api.request.AddShopCarRequest
import com.beijiteshop.shop.model.api.request.GetGoodCountRequest
import com.beijiteshop.shop.ui.shopcar.repo.ShopCarRepo

/**
 * @Author LiaoZhongKai
 * @Date 2021/3/19 15:59
 * @Description
 */
class ShopCarVM(private val repo: ShopCarRepo) : BaseViewModel() {

    val liveAddToShopCar = MutableLiveData<Pair<Boolean,String?>>()
    val liveShopCarList = MutableLiveData<MutableList<ShopCarProductEntity>>()
    val liveDeleteGoods =  MutableLiveData<Pair<Boolean,String?>>()
    val liveChangeCount = MutableLiveData<Pair<Boolean,Int>>()
    val liveChangeCountFailed = MutableLiveData<String?>()
    val liveGoodsCount = MutableLiveData<Int>()

    fun addToShopCar(request: AddShopCarRequest) = launch {
        repo.addToShopCar(request)
            .serverData()
            .onSuccess {
                onBizOk { msg, _ ->
                    liveAddToShopCar.postValue(Pair(true,msg))
                }

                onBizError { _, msg ->
                    liveAddToShopCar.postValue(Pair(false,msg))
                }
            }
            .onFailure {
                liveAddToShopCar.postValue(Pair(false,it.message))
            }
    }


    fun getShopCarList(page: Int) = launch {
        repo.getShopCarList(page)
            .serverData()
            .onSuccess {
                onBizOK { _, data, _ ->
                    if (data?.data.isNullOrEmpty()){
                        liveEmpty.postValue(null)
                    }else{
                        val list = data!!.data!!
                        val result = mutableListOf<ShopCarProductEntity>()
                        list.forEach {
                            val entity = ShopCarProductEntity(false, ObservableInt(it.count),it)
                            result.add(entity)
                        }
                        liveShopCarList.postValue(result)
                    }
                }

                onBizError { _, message ->
                    liveError.postValue(message)
                }

                onBizEmpty {
                    liveEmpty.postValue(it)
                }
            }
            .onFailure {
                liveNetError.postValue(it.message)
            }
    }

    fun deleteGoods(id: String) = launch {
        repo.deleteGoods(id)
            .serverData()
            .onSuccess {
                onBizOk { _, _ ->
                    liveDeleteGoods.postValue(Pair(true,id))
                }

                onBizError { _, msg ->
                    liveDeleteGoods.postValue(Pair(false,msg))
                }
            }
            .onFailure {
                liveDeleteGoods.postValue(Pair(false,it.message))
            }
    }

    fun addCount(id: String,position: Int) = launch {
        repo.addCount(id)
            .serverData()
            .onSuccess {
                onBizOk { _, _ ->
                    liveChangeCount.postValue(Pair(true,position))
                }

                onBizError { _, msg ->
                    liveChangeCountFailed.postValue(msg)
                }
            }
            .onFailure {
                liveChangeCountFailed.postValue(it.message)
            }
    }

    fun reduceCount(id: String,position: Int) = launch {
        repo.reduceCount(id)
            .serverData()
            .onSuccess {
                onBizOk { _, _ ->
                    liveChangeCount.postValue(Pair(false,position))
                }

                onBizError { _, msg ->
                    liveChangeCountFailed.postValue(msg)
                }
            }
            .onFailure {
                liveChangeCountFailed.postValue(it.message)
            }
    }

    /**
     * 获取单个商品在购物车的数量
     */
    fun getGoodCount(busGoodId: String,color: String?,specification: String) = launch {
        repo.getGoodCount(busGoodId, color, specification)
            .serverData()
            .onSuccess {
                onBizOK { _, data, _ ->
                    liveGoodsCount.postValue(data?.count)
                }

                onBizError { _, _ ->
                    liveGoodsCount.postValue(0)
                }
            }
    }
}